# dplyr, magrittr, tidyr
library(magrittr)
From Liselotte Diaz Högberg (Liselotte.Diaz-Hogberg@ecdc.europa.eu), on 2019-02-18:
EARS-Net data are available for public download through our Surveillance Atlas, atlas.ecdc.europa.eu/public (choose Antimicrobial resistance). The atlas includes a data export option in the top right corner. This data are open for use as long as the source is acknowledged. As a researcher, you are also welcome to access EARS-Net data through a third party data access request. Please find more information here ecdc.europa.eu/en/publications-data/european-surveillance-system-tessy and do not hesitate to contact me for further details and clarifications.
We select All time periods, All regions, All indicators that we export to the ECDC_surveillance_data_Antimicrobial_resistance.csv file on 2019-10-03
ecdc <- "ECDC_surveillance_data_Antimicrobial_resistance.csv" %>%
read.csv() %>%
dplyr::filter(Unit == "N") %>%
dplyr::select(-HealthTopic, -Unit, -RegionCode, -TxtValue) %>%
tidyr::separate(Population, c("bacteria", "test"), "\\|") %>%
dplyr::filter(!grepl("Combined", test)) %>%
dplyr::mutate_if(is.factor, as.character) %>%
dplyr::mutate_at("NumValue", as.integer) %>%
tidyr::pivot_wider(names_from = Indicator, values_from = NumValue) %>%
dplyr::transmute(bacteria = bacteria,
test = test,
year = Time,
country = RegionName,
N = `Total tested isolates`,
R = `Resistant (R) isolates`,
I = `Non-susceptible (I and R) isolates` - R)
which gives:
ecdc
## # A tibble: 10,925 x 7
## bacteria test year country N R I
## <chr> <chr> <int> <chr> <int> <int> <int>
## 1 Acinetobacter spp. Aminoglycosides 2012 Austria 0 0 0
## 2 Acinetobacter spp. Aminoglycosides 2012 Belgium 0 0 0
## 3 Acinetobacter spp. Aminoglycosides 2012 Bulgaria 65 38 6
## 4 Acinetobacter spp. Aminoglycosides 2012 Cyprus 23 12 0
## 5 Acinetobacter spp. Aminoglycosides 2012 Czech Republic 0 0 0
## 6 Acinetobacter spp. Aminoglycosides 2012 Germany 119 7 0
## 7 Acinetobacter spp. Aminoglycosides 2012 Denmark 77 8 0
## 8 Acinetobacter spp. Aminoglycosides 2012 Estonia 0 0 0
## 9 Acinetobacter spp. Aminoglycosides 2012 Greece 1234 964 118
## 10 Acinetobacter spp. Aminoglycosides 2012 Spain 0 0 0
## # … with 10,915 more rows
where N is the total number of samples, R is the number of samples that are resistant and I is the number of samples for which the resistance level is intermediate.
The ECDC data has data on 8 bacteria:
sort(unique(ecdc$bacteria))
## [1] "Acinetobacter spp." "Enterococcus faecalis" "Enterococcus faecium" "Escherichia coli"
## [5] "Klebsiella pneumoniae" "Pseudomonas aeruginosa" "Staphylococcus aureus" "Streptococcus pneumoniae"
in 30 countries:
sort(unique(ecdc$country))
## [1] "Austria" "Belgium" "Bulgaria" "Croatia" "Cyprus" "Czech Republic" "Denmark"
## [8] "Estonia" "Finland" "France" "Germany" "Greece" "Hungary" "Iceland"
## [15] "Ireland" "Italy" "Latvia" "Lithuania" "Luxembourg" "Malta" "Netherlands"
## [22] "Norway" "Poland" "Portugal" "Romania" "Slovakia" "Slovenia" "Spain"
## [29] "Sweden" "United Kingdom"
over 18 years from 2000 to 2017
The tests of susceptibility that are performed are the following for the different bacteria:
with(ecdc, table(bacteria, test))
## test
## bacteria Aminoglycosides Aminopenicillins Carbapenems Ceftazidime Fluoroquinolones High-level gentamicin Macrolides
## Acinetobacter spp. 180 0 180 0 180 0 0
## Enterococcus faecalis 0 511 0 0 0 511 0
## Enterococcus faecium 0 511 0 0 0 510 0
## Escherichia coli 512 512 510 0 512 0 0
## Klebsiella pneumoniae 386 0 386 0 386 0 0
## Pseudomonas aeruginosa 386 0 386 386 386 0 0
## Staphylococcus aureus 0 0 0 0 0 0 0
## Streptococcus pneumoniae 0 0 0 0 0 0 386
## test
## bacteria Meticillin (MRSA) Penicillins PiperacillinTazobactam Third-generation cephalosporins Vancomycin
## Acinetobacter spp. 0 0 0 0 0
## Enterococcus faecalis 0 0 0 0 511
## Enterococcus faecium 0 0 0 0 511
## Escherichia coli 0 0 0 512 0
## Klebsiella pneumoniae 0 0 0 386 0
## Pseudomonas aeruginosa 0 0 386 0 0
## Staphylococcus aureus 516 0 0 0 0
## Streptococcus pneumoniae 0 386 0 0 0
The number of biological tests performed in the countries naturally increases with time:
with(ecdc, table(year, country))
## country
## year Austria Belgium Bulgaria Croatia Cyprus Czech Republic Denmark Estonia Finland France Germany Greece Hungary Iceland Ireland
## 2000 12 12 1 0 0 1 12 4 12 0 12 12 0 12 12
## 2001 12 12 12 12 0 12 12 12 12 12 12 12 12 12 12
## 2002 12 12 12 12 0 12 12 12 12 12 12 12 12 12 12
## 2003 12 12 12 12 11 12 12 12 12 12 12 12 12 12 12
## 2004 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12
## 2005 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23
## 2006 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23
## 2007 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23
## 2008 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23
## 2009 23 23 23 0 23 23 23 23 23 23 23 23 23 23 23
## 2010 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23
## 2011 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23
## 2012 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26
## 2013 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26
## 2014 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26
## 2015 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26
## 2016 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26
## 2017 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26
## country
## year Italy Latvia Lithuania Luxembourg Malta Netherlands Norway Poland Portugal Romania Slovakia Slovenia Spain Sweden United Kingdom
## 2000 12 0 0 12 1 12 12 0 12 0 0 1 12 12 12
## 2001 12 0 0 12 12 12 12 12 12 0 11 12 12 12 12
## 2002 12 0 0 12 12 12 12 12 12 12 12 12 12 12 12
## 2003 12 0 0 12 12 12 12 12 12 12 12 12 12 12 12
## 2004 12 1 0 12 12 12 12 12 12 12 12 12 12 12 12
## 2005 23 23 0 23 23 23 23 23 23 23 23 23 23 23 23
## 2006 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23
## 2007 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23
## 2008 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23
## 2009 23 23 23 23 23 23 23 23 23 23 0 23 23 23 23
## 2010 23 23 23 23 23 23 23 23 23 23 0 23 23 23 23
## 2011 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23
## 2012 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26
## 2013 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26
## 2014 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26
## 2015 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26
## 2016 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26
## 2017 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26
A function that estimates proportion with confidence intervals:
proportion <- function(x, n, ...) {
if (n < 1) return(rep(0, 3))
with(binom.test(x, n, ...), c(estimate, conf.int))
}
Using this function to add proportion to all the data:
(ecdc2 <- ecdc %>%
dplyr::mutate(prop = purrr::map2(R, N, proportion)) %>%
tidyr::unnest(prop) %>%
dplyr::mutate(., names = rep_len(c("estimate", "lower", "upper"), nrow(.))) %>%
tidyr::pivot_wider(names_from = names, values_from = prop))
## # A tibble: 10,925 x 10
## bacteria test year country N R I estimate lower upper
## <chr> <chr> <int> <chr> <int> <int> <int> <dbl> <dbl> <dbl>
## 1 Acinetobacter spp. Aminoglycosides 2012 Austria 0 0 0 0 0 0
## 2 Acinetobacter spp. Aminoglycosides 2012 Belgium 0 0 0 0 0 0
## 3 Acinetobacter spp. Aminoglycosides 2012 Bulgaria 65 38 6 0.585 0.456 0.706
## 4 Acinetobacter spp. Aminoglycosides 2012 Cyprus 23 12 0 0.522 0.306 0.732
## 5 Acinetobacter spp. Aminoglycosides 2012 Czech Republic 0 0 0 0 0 0
## 6 Acinetobacter spp. Aminoglycosides 2012 Germany 119 7 0 0.0588 0.0240 0.117
## 7 Acinetobacter spp. Aminoglycosides 2012 Denmark 77 8 0 0.104 0.0459 0.194
## 8 Acinetobacter spp. Aminoglycosides 2012 Estonia 0 0 0 0 0 0
## 9 Acinetobacter spp. Aminoglycosides 2012 Greece 1234 964 118 0.781 0.757 0.804
## 10 Acinetobacter spp. Aminoglycosides 2012 Spain 0 0 0 0 0 0
## # … with 10,915 more rows
Let’s plot it:
ecdc2 %>%
dplyr::filter(bacteria == "Escherichia coli",
test == "Third-generation cephalosporins",
country == "Netherlands") %>%
dplyr::arrange(year) %>%
with({
plot(year, estimate, pch = 19, ylim = c(0, max(upper)), col = "red",
type = "o", lty = 2, xlab = NA, ylab = "proportion of resistance")
arrows(year, lower, year, upper, .05, 90, 3, col = "red")
})
Let’s make a function of this pipeline:
plot_resistance <- function(df, bug, drug, country, ylim = NULL) {
require(magrittr)
df %<>% dplyr::filter(bacteria == !! bug,
test == !! drug,
country == !! country)
if (is.null(ylim)) ylim <- c(0, max(df$upper))
df %>%
dplyr::arrange(year) %>%
with({
plot(year, estimate, pch = 19, ylim = ylim, col = "red",
type = "o", lty = 2, xlab = NA, ylab = "proportion of resistance")
arrows(year, lower, year, upper, .05, 90, 3, col = "red")
})
}
And include this function into another one that plot the resistance levels of a given bacteria and a given antimicriobial for all the countries:
plot_resistances <- function(df, bug, drug, ylim = NULL) {
opar <- par(mfrow = c(10, 3), plt = c(.15, .99, .2, .9))
for (country in sort(unique(ecdc$country))) {
plot_resistance(df, bug, drug, country, ylim)
mtext(country)
}
par(opar)
}
Let’s use this function for a number of bacteria / drug combinations:
plot_resistances(ecdc2, "Escherichia coli", "Third-generation cephalosporins")
The same, this time with the same scale on the \(y\)-axis:
plot_resistances(ecdc2, "Escherichia coli", "Third-generation cephalosporins", c(0, .5))
plot_resistances(ecdc2, "Klebsiella pneumoniae", "Third-generation cephalosporins", c(0, 1))
plot_resistances(ecdc2, "Klebsiella pneumoniae", "Carbapenems", c(0, 1))
plot_resistances(ecdc2, "Pseudomonas aeruginosa", "Carbapenems", c(0, 1))
plot_resistances(ecdc2, "Pseudomonas aeruginosa", "Ceftazidime", c(0, 1))
plot_resistances(ecdc2, "Staphylococcus aureus", "Meticillin (MRSA)", c(0, 1))
A Poisson model:
\[ R_{y+1} \sim \mbox{Pois}\left(\left[R_y + \rho R_y \left(N_y - \frac{R_y}{N_y}\right)\right]\frac{N_{y + 1}}{N_y}\right) \]
A binomial model:
\[ R_{y+1} \sim \mbox{Bin}\left(N_{y+1}, \frac{R_y}{N_y} + \rho R_y \left(1 - \frac{R_y}{N_y^2}\right)\right) \]